package com.ruoyi.flowable.config;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.cmmn.converter.CmmnXmlConverter;
import org.flowable.cmmn.editor.json.converter.CmmnJsonConverter;
import org.flowable.common.engine.impl.cfg.IdGenerator;
import org.flowable.common.engine.impl.history.HistoryLevel;
import org.flowable.dmn.editor.converter.DmnJsonConverter;
import org.flowable.dmn.xml.converter.DmnXMLConverter;
import org.flowable.editor.language.json.converter.BpmnJsonConverter;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.ui.common.rest.idm.remote.RemoteAccountResource;
import org.flowable.ui.modeler.properties.FlowableModelerAppProperties;
import org.flowable.ui.modeler.rest.app.EditorGroupsResource;
import org.flowable.ui.modeler.rest.app.EditorUsersResource;
import org.flowable.ui.modeler.rest.app.ModelResource;
import org.flowable.ui.modeler.rest.app.ModelsResource;
import org.flowable.validation.ProcessValidatorFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.transaction.PlatformTransactionManager;

//todo 添加流程时会用到用户id，重构 SecurityUtils.getCurrentUserObject 获取用户信息，这个文件路径必须和原路径一致,参考网址 https://blog.51cto.com/14622073/2502873
@Configuration
@ComponentScan(basePackages = {
        "org.flowable.ui.modeler.repository",
        "org.flowable.ui.modeler.service",
        "org.flowable.ui.modeler.rest",
        "org.flowable.ui.common.rest.exception",
        "org.flowable.ui.common.rest",
        "org.flowable.ui.idm.service" },
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = EditorGroupsResource.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = EditorUsersResource.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ModelsResource.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = RemoteAccountResource.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ModelResource.class),
        })
@EnableConfigurationProperties(value = { FlowableModelerAppProperties.class })
public class FlowableConfig {

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, IdGenerator generator) {
        SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
        conf.setDataSource(dataSource);
        conf.setDeploymentName("RuoYiFlowable");
        conf.setIdGenerator(generator);
        conf.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        conf.setDatabaseType(SpringProcessEngineConfiguration.DATABASE_TYPE_MYSQL);
        conf.setEnableLogSqlExecutionTime(true);
        Set<String> stringSet = new HashSet<>();
        stringSet.add("mapper/flowable/Model_EXT_Mapper.xml");
        conf.setCustomMybatisXMLMappers(stringSet);
        // 因为使用自定义SqlSessionFactory，SpringProcessEngineConfiguration 中设置 databaseTablePrefix 无效
        conf.setDatabaseTablePrefix("");
        conf.setActivityFontName("宋体");
        conf.setLabelFontName("宋体");
        conf.setAnnotationFontName("宋体");
        conf.setHistoryLevel(HistoryLevel.FULL);
        return conf;
    }

    @Bean
    public SpringAppEngineConfiguration springAppEngineConfiguration(DataSource dataSource, IdGenerator generator,
            PlatformTransactionManager platformTransactionManager) {
        SpringAppEngineConfiguration conf = new SpringAppEngineConfiguration();
        conf.setDataSource(dataSource);
        conf.setIdGenerator(generator);
        conf.setTransactionManager(platformTransactionManager);
        conf.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        conf.setDatabaseType(SpringProcessEngineConfiguration.DATABASE_TYPE_MYSQL);
        conf.setEnableLogSqlExecutionTime(true);
        Set<String> stringSet = new HashSet<>();
        stringSet.add("mapper/flowable/Model_EXT_Mapper.xml");
        conf.setCustomMybatisXMLMappers(stringSet);
        // 因为使用自定义SqlSessionFactory，SpringProcessEngineConfiguration 中设置 databaseTablePrefix 无效
        conf.setDatabaseTablePrefix("");
        conf.setAppEngineName("RuoYiFlowable");
        return conf;
    }

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(DataSource dataSource, IdGenerator generator,
            PlatformTransactionManager platformTransactionManager,
            EntityManagerFactory entityManagerFactory) {
        SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
        conf.setDataSource(dataSource);
        conf.setIdGenerator(generator);
        conf.setTransactionManager(platformTransactionManager);
        conf.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        conf.setDatabaseType(SpringProcessEngineConfiguration.DATABASE_TYPE_MYSQL);
        conf.setEnableLogSqlExecutionTime(true);
        Set<String> stringSet = new HashSet<>();
        stringSet.add("mapper/flowable/Model_EXT_Mapper.xml");
        conf.setCustomMybatisXMLMappers(stringSet);
        // 因为使用自定义SqlSessionFactory，SpringProcessEngineConfiguration 中设置 databaseTablePrefix 无效
        conf.setDatabaseTablePrefix("");
        //jpa配置
        conf.setJpaCloseEntityManager(false);
        conf.setJpaHandleTransaction(false);
        conf.setJpaEntityManagerFactory(entityManagerFactory);
        //email设置
        conf.setMailServerHost("smtp.qq.com");
        conf.setMailServerPort(465);
        conf.setMailServerDefaultFrom("1447807914@qq.com");
        conf.setMailServerUsername("1447807914@qq.com");
        conf.setMailServerPassword("tcyukxlhrnjjffhb");
        conf.setMailServerUseSSL(true);

        return conf;
    }

    @Bean
    public BpmnJsonConverter getBpmnJsonConverter() {
        return new BpmnJsonConverter();
    }

    @Bean
    public BpmnXMLConverter getBpmnXMLConverter() {
        return new BpmnXMLConverter();
    }

    @Bean
    public ProcessValidatorFactory getProcessValidatorFactory() {
        return new ProcessValidatorFactory();
    }

    @Bean
    public CmmnXmlConverter getCmmnXmlConverter() {
        return new CmmnXmlConverter();
    }

    @Bean
    public CmmnJsonConverter getCmmnJsonConverter() {
        return new CmmnJsonConverter();
    }

    @Bean
    public DmnXMLConverter getDmnXMLConverter() {
        return new DmnXMLConverter();
    }

    @Bean
    public DmnJsonConverter getDmnJsonConverter() {
        return new DmnJsonConverter();
    }

}
